import { useProjectStore } from "/@/stores/project";
const projectStore = useProjectStore();
let scriptRegex = /<script[^>]*?type="module"[^>]*>([\s\S]*?)<\/script>/gi;
let oldScirpt = null;
let getScirpt = async () => {
  let res = await fetch("/");
  let text = await res.text();
  let matches = text.match(scriptRegex);
  if (matches && matches.length > 0) {
    return matches;
  } else {
    return [];
  }
};

let isUpdate = async () => {
  let res = await getScirpt();
  if (res.length == 0) {
    return false;
  }
  if (!oldScirpt) {
    oldScirpt = res;
    return false;
  }

  let newScirpt = res;
  let diff = newScirpt.filter((item) => oldScirpt.includes(item));

  if (diff.length !== oldScirpt.length) {
    return true;
  } else {
    return false;
  }
};

let autoGet = () => {
  setTimeout(async () => {
    let bool = await isUpdate();
    projectStore.autoReLoad = bool;
    autoGet();
  }, 30000);
};

autoGet();
